package cn.leedsoft.luma.web_service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.ImmutableMap;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.Map;

/**
 * Created by leeds on 2016/11/9 9:35.
 */
public class AlidayuHelper {
    private static final Logger logger = LoggerFactory.getLogger(AlidayuHelper.class);

    //alidayu
    private static final String ENDPOINT = "http://gw.api.taobao.com/router/rest";
    private static final String APPKEY = "23272887";
    private static final String SECRET = "257a1644cdc590ea3dca622e36884d8e";


    private static TaobaoClient client = new DefaultTaobaoClient(ENDPOINT, APPKEY, SECRET);


    public static class AlidayuSmsParamVo {
        private String sign;//签名
        private String tplCode;//模板
        private Map<String, Object> tplFields;//模板变量
        private String cellphone;//手机号
        private String ip;

        public String getSign() {
            return sign;
        }

        public void setSign(String sign) {
            this.sign = sign;
        }

        public String getTplCode() {
            return tplCode;
        }

        public void setTplCode(String tplCode) {
            this.tplCode = tplCode;
        }

        public Map<String, Object> getTplFields() {
            return tplFields;
        }

        public void setTplFields(Map<String, Object> tplFields) {
            this.tplFields = tplFields;
        }

        public String getCellphone() {
            return cellphone;
        }

        public void setCellphone(String cellphone) {
            this.cellphone = cellphone;
        }

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }
    }

    public enum  AlidayuSmsResultDto {
        SUCCESS(0, "短信发送成功"), FAIL_API(10, "短信接口调用失败"),
        FAIL_LIMIT_PHONE(11, "同一手机发送短信超过限制"), FAIL_LIMIT_IP(12, "同一IP发送短信超过限制");
        private int code;
        private String name;
        AlidayuSmsResultDto(int code, String name) {
            this.code = code;
            this.name = name;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public static AlidayuSmsResultDto sendSms(AlidayuSmsParamVo paramDto) {
        Assert.notNull(paramDto, "短信接口参数不可为空");
        //todo cellphone、ip是否超过限制

        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setSmsType("normal");//短信类型
        req.setSmsFreeSignName(paramDto.getSign());//签名
        req.setSmsTemplateCode(paramDto.getTplCode());//模板
        req.setSmsParam(JSON.toJSONString(paramDto.tplFields));//模板变量
        req.setRecNum(paramDto.getCellphone());//cellphone

        AlibabaAliqinFcSmsNumSendResponse res = null;
        try {
            res = client.execute(req);
            logger.debug(">>>>返回结果: " + res.getBody());
            if (res.getResult() != null && res.getResult().getSuccess()) {
                logger.info(">>>>短信接口调用成功: ");
                return AlidayuSmsResultDto.SUCCESS;
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
        logger.error(">>>>短信接口调用失败： " + (res != null ? res.getSubMsg() : "未知错误"));
        return AlidayuSmsResultDto.FAIL_API;
    }

    public static void main(String[] args) {
        AlidayuSmsParamVo vo = new AlidayuSmsParamVo();
        vo.setSign("大鱼测试");
        vo.setTplCode("SMS_6794367");
        vo.setTplFields(ImmutableMap.of("customer", "1236"));
        vo.setCellphone("18612115940");

        AlidayuSmsResultDto dto = AlidayuHelper.sendSms(vo);
        System.out.println(dto);
    }
}
